package cn.stylefeng.guns.modular.bs.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.bs.entity.BsWarehousePro;
import cn.stylefeng.guns.modular.bs.model.params.BsWarehouseProParam;
import cn.stylefeng.guns.modular.bs.service.BsWarehouseProService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 大屏-仓库信息控制器
 *
 * @author zhangzh
 * @Date 2025-07-15 09:49:29
 */
@Controller
@RequestMapping("/bsWarehousePro")
public class BsWarehouseProController extends BaseController {

    private String PREFIX = "/bs/bsWarehousePro";

    @Autowired
    private BsWarehouseProService bsWarehouseProService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/bsWarehousePro.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/bsWarehousePro_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/bsWarehousePro_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BsWarehouseProParam bsWarehouseProParam) {

        if(ToolUtil.isNotEmpty(bsWarehouseProParam.getProId())){
            BasicOperate basicOperate = basicOperateService.getById(bsWarehouseProParam.getProId());
            if(ToolUtil.isNotEmpty(basicOperate)){
                bsWarehouseProParam.setProName(basicOperate.getOperateName());
            }
        }

        if(ToolUtil.isNotEmpty(bsWarehouseProParam.getWarehouseId())){
            BasicWarehouse basicWarehouse = basicWarehouseService.getById(bsWarehouseProParam.getWarehouseId());
            if(ToolUtil.isNotEmpty(basicWarehouse)){
                bsWarehouseProParam.setWarehouseName(basicWarehouse.getWarehouseName());
            }
        }

        this.bsWarehouseProService.add(bsWarehouseProParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BsWarehouseProParam bsWarehouseProParam) {
        if(ToolUtil.isNotEmpty(bsWarehouseProParam.getProId())){
            BasicOperate basicOperate = basicOperateService.getById(bsWarehouseProParam.getProId());
            if(ToolUtil.isNotEmpty(basicOperate)){
                bsWarehouseProParam.setProName(basicOperate.getOperateName());
            }
        }

        if(ToolUtil.isNotEmpty(bsWarehouseProParam.getWarehouseId())){
            BasicWarehouse basicWarehouse = basicWarehouseService.getById(bsWarehouseProParam.getWarehouseId());
            if(ToolUtil.isNotEmpty(basicWarehouse)){
                bsWarehouseProParam.setWarehouseName(basicWarehouse.getWarehouseName());
            }
        }
        this.bsWarehouseProService.update(bsWarehouseProParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BsWarehouseProParam bsWarehouseProParam) {
        this.bsWarehouseProService.delete(bsWarehouseProParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BsWarehouseProParam bsWarehouseProParam) {
        BsWarehousePro detail = this.bsWarehouseProService.getById(bsWarehouseProParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2025-07-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BsWarehouseProParam bsWarehouseProParam,String condition ) {
        return this.bsWarehouseProService.findPageBySpec(bsWarehouseProParam,condition);
    }

}


